package cn.com.tuns.assess.qsb.frame.util;

import android.text.TextUtils;
import android.text.format.Time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    public final static String BzFormat="yyyy-MM-dd HH:mm:ss";
    public final static String BzFormatShort="yyyyMMddHHmmss";
    public final static String YmdFormat="yyyy-MM-dd";
    public final static String HmsFormat="HH:mm:ss";

    private static final String[] WEEKS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    private static final Time time = new Time();
    private static final String[][] provinceArray = {{"北京市", "京"},
            {"天津市", "津"},
            {"河北省", "冀"},
            {"山西省", "晋"},
            {"内蒙古自治区", "蒙"},
            {"辽宁省", "辽"},
            {"吉林省", "吉"},
            {"黑龙江省", "黑"},
            {"上海市", "沪"},
            {"江苏省", "苏"},
            {"浙江省", "浙"},
            {"安徽省", "皖"},
            {"福建省", "闽"},
            {"江西省", "赣"},
            {"山东省", "鲁"},
            {"河南省", "豫"},
            {"湖北省", "鄂"},
            {"湖南省", "湘"},
            {"广东省", "粤"},
            {"广西壮族自治区", "桂"},
            {"海南省", "琼"},
            {"重庆市", "渝"},
            {"四川省", "川"},
            {"贵州省", "贵"},
            {"云南省", "云"},
            {"西藏自治区", "藏"},
            {"陕西省", "陕"},
            {"甘肃省", "甘"},
            {"青海省", "青"},
            {"宁夏回族自治区", "宁"},
            {"新疆维吾尔自治区", "新"}};

    public static String getProvinceBrief(String province) {
        for (String[] s : provinceArray) {
            if (s[0].equals(province)) {
                return s[1];
            }
        }
        return null;


    }


    /**
     * 比较两个日期的大小，日期格式为yyyy-MM-dd
     *
     * @param str1 the first date
     * @param str2 the second date
     * @return true <br/>false
     */
    public static boolean compare(String str1, String str2) {
        boolean isBigger = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt1 = null;
        Date dt2 = null;
        try {
            dt1 = sdf.parse(str1);
            dt2 = sdf.parse(str2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dt1.getTime() > dt2.getTime()) {
            isBigger = true;
        } else if (dt1.getTime() < dt2.getTime()) {
            isBigger = false;
        }
        return isBigger;
    }

    //获得明天日期
    public static String getTomoData() {

        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DATE,1);

        return dateToString(calendar.getTime(),YmdFormat);
    }


    // string类型转换为date类型
    // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒，
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        try {
            date = formatter.parse(strTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

    // date类型转换为String类型
    // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    // data Date类型的时间
    public static String dateToString(Date data, String formatType) {
        if(data==null)
            data=new Date();
        if(TextUtils.isEmpty(formatType))
            formatType=BzFormat;
        return new SimpleDateFormat(formatType).format(data);
    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     * @param date1
     * @param date2
     * date1-date2
     * * @return
     */
    public static int differentDays(Date date1,Date date2)
    {
        int day=1000*3600*24;
        int days = (int) (date1.getTime()/day - date2.getTime()/day) ;
        return days;
    }

    public static Date addDays(Date date,int days){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(days,Calendar.DATE);
        return calendar.getTime();
    }



}